library(survey)
library(dplyr)
library(ggplot2)
library(gridExtra)
library(RColorBrewer)

# Figure 8.10
summary(factor(df$Q11))
summary(factor(df$Q2))

# A great deal
df.gd <- df[df$Q11 == 1 & !is.na(df$Q2),]
df.gd <- df.gd[df.gd$Q2 < 77,]

# Quite a lot
df.qa <- df[df$Q11 == 2 & !is.na(df$Q2),]
df.qa <- df.qa[df.qa$Q2 < 77,]

# Some
df.s <- df[df$Q11 == 3 & !is.na(df$Q2),]
df.s <- df.s[df.s$Q2 < 77,]

# Very little
df.vl <- df[df$Q11 == 4 & !is.na(df$Q2),]
df.vl <- df.vl[df.vl$Q2 < 77,]

# Create Survey design
# A great deal
w2_design.gd <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df.gd
  )

# Quite a lot
w2_design.qa <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df.qa
  )

# Some
w2_design.s <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df.s
  )

# Very little
w2_design.vl <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df.vl
  )

# A great deal
Q2_gd <- svytable(~Q2,w2_design.gd)
df.gd <- data.frame(budget=factor(c("$20 Billion","$55 Billion","$250 Billion",
                                "$450 Billion","$700 Billion",'$1.1 Trillion'), 
                                levels=c("$20 Billion","$55 Billion","$250 Billion",
                                         "$450 Billion","$700 Billion",'$1.1 Trillion')),
                       percent=round(Q2_gd/sum(Q2_gd)*100))

# Quite a lot
Q2_qa <- svytable(~Q2,w2_design.qa)
df.qa <- data.frame(budget=factor(c("$20 Billion","$55 Billion","$250 Billion",
                                    "$450 Billion","$700 Billion",'$1.1 Trillion'), 
                                  levels=c("$20 Billion","$55 Billion","$250 Billion",
                                           "$450 Billion","$700 Billion",'$1.1 Trillion')),
                    percent=round(Q2_qa/sum(Q2_qa)*100))


# Some
Q2_s <- svytable(~Q2,w2_design.s)
df.s <- data.frame(budget=factor(c("$20 Billion","$55 Billion","$250 Billion",
                                    "$450 Billion","$700 Billion",'$1.1 Trillion'), 
                                  levels=c("$20 Billion","$55 Billion","$250 Billion",
                                           "$450 Billion","$700 Billion",'$1.1 Trillion')),
                    percent=round(Q2_s/sum(Q2_s)*100))


# Very little
Q2_vl <- svytable(~Q2,w2_design.vl)
df.vl <- data.frame(budget=factor(c("$20 Billion","$55 Billion","$250 Billion",
                                    "$450 Billion","$700 Billion",'$1.1 Trillion'), 
                                  levels=c("$20 Billion","$55 Billion","$250 Billion",
                                           "$450 Billion","$700 Billion",'$1.1 Trillion')),
                    percent=round(Q2_vl/sum(Q2_vl)*100))


# Pie Charts

g.gd <- ggplot(df.gd,aes(x="",y=percent.Freq,fill=budget))+
  geom_bar(stat="identity",width=1,color='white')+
  coord_polar("y",start=0)+
  scale_fill_brewer(palette='Blues')+
  theme_void()+
  geom_text(aes(y=c(95.5,87,72,53,33.5,11),label=percent.Freq),color='black',size=4.5)+
  ggtitle("A great deal")+
  theme(legend.position = "null",plot.title = element_text(hjust=0.5))
  
g.qa <- ggplot(df.qa,aes(x="",y=percent.Freq,fill=budget))+
  geom_bar(stat="identity",width=1,color='white')+
  coord_polar("y",start=0)+
  scale_fill_brewer(palette='Blues')+
  theme_void()+
  geom_text(aes(y=c(95,85,71,52,30,9),label=percent.Freq),color='black',size=4.5)+
  ggtitle("Quite a lot")+
  theme(legend.position = "null",plot.title = element_text(hjust=0.5))

g.s <- ggplot(df.s,aes(x="",y=percent.Freq,fill=budget))+
  geom_bar(stat="identity",width=1,color='white')+
  coord_polar("y",start=0)+
  scale_fill_brewer(palette='Blues')+
  theme_void()+
  geom_text(aes(y=c(93.5,82,68,51.5,34.5,12.5),label=percent.Freq),color='black',size=4.5)+
  ggtitle("Some")+
  theme(legend.position = "null",plot.title = element_text(hjust=0.5))

g.vl <- ggplot(df.vl,aes(x="",y=percent.Freq,fill=budget))+
  geom_bar(stat="identity",width=1,color='white')+
  coord_polar("y",start=0)+
  scale_fill_brewer(palette='Blues')+
  theme_void()+
  geom_text(aes(y=c(93,84,74.5,63,48,18.5),label=percent.Freq),color='black',size=4.5)+
  ggtitle("Very little")+
  theme(legend.position = c(-.12,0.2),plot.title = element_text(hjust=0.5))

grid.arrange(g.gd,g.qa,g.s,g.vl,ncol=2,nrow=2,widths=c(1,1))
